﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Common;


namespace YunCMS.Web.Admin.Catogory
{
    public partial class CategoryList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string action = Request.QueryString["action"];
                int id;
                string module = Request.QueryString["module"];
                if (string.IsNullOrEmpty(module))
                {
                    JScript.Alert("模块选择不正确", this);
                    return;
                }
                else
                {
                    ViewState["module"] = module;
                }

                if (!string.IsNullOrEmpty(action))
                {
                    string _id = Request.QueryString["id"];

                    if (!int.TryParse(_id, out id))
                        id = 0;
                    if (id != 0)
                        HandleAction(action, id);
                }
                Databind(module);
            }
        }

        private void Databind(string moduleType)
        {
            IList<YCCategory> list = new List<YCCategory>();
            YCCategoryBLL bll = new YCCategoryBLL();
            list = bll.GetList(moduleType);
            DataListRP.DataSource = list;
            DataListRP.DataBind();
        }

        private void HandleAction(string action, int id)
        {
            YCCategoryBLL bll = new YCCategoryBLL();
            if (action == "delete")
            {
                bll.Remove(id);
            }
            else if (action == "active") {
                string qval = Request.QueryString["val"];
                if (!string.IsNullOrEmpty(qval))
                {
                    bool val = Convert.ToBoolean(qval);
                    bll.UpdateActive(id, !val);
                }
            }
        }

        private IList<int> AllSelectedItems()
        {
            List<int> ids = new List<int>() { };
            for (int i = 0; i < DataListRP.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)DataListRP.Items[i].FindControl("idCHB");
                if (cb.Checked)
                {
                    int id = Convert.ToInt32(((Label)DataListRP.Items[i].FindControl("idLabel")).Text);
                    ids.Add(id);
                }
            }
            return ids;
        }

        protected void deleteAllBT_Click(object sender, EventArgs e)
        {
            YCCategoryBLL bll = new YCCategoryBLL();
            IList<int> ids = AllSelectedItems();
            foreach (int id in ids)
            {
                bll.Remove(id);
            }
            string module = ViewState["module"].ToString();
            if (!string.IsNullOrEmpty(module))
            {
                Databind(module);
            }
        }

        protected void DataListRP_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal BranchImg = (Literal)e.Item.FindControl("BranchImg");
                HiddenField levelHdn = (HiddenField)e.Item.FindControl("levelHdn");
                string litStyle = "<span style=width:{0}px;text-align:right;display:inline-block;>{1}{2}</span>";
                string litImg1 = "<img src=../images/folder_open.gif align=absmiddle />";
                string litImg2 = "<img src=../images/t.gif align=absmiddle />";

                int classLayer = Convert.ToInt32(levelHdn.Value);
                if (classLayer == 1)
                {
                    BranchImg.Text = litImg1;
                }
                else
                {
                    BranchImg.Text = string.Format(litStyle, classLayer * 18, litImg2, litImg1);
                }
            }
        }
    }
}